﻿$directoryPath = "./Web"
$absolutePath = Resolve-Path $directoryPath

# 1. 初始化配置对象数组
$configs = @(
    @{ Name = "ApiManage";      Port = 8016; Version = "v4.0"; Mode = "Classic";    Type = 3 }
    @{ Name = "AuthCenterAPI";  Port = 8015; Version = "v4.0"; Mode = "Classic";    Type = 3 }
    @{ Name = "Base";           Port = 8001; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "BaseAPI";        Port = 7001; Version = "v4.0"; Mode = "Integrated"; Type = 3 }
    @{ Name = "CIS";            Port = 8003; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "CISAPI";         Port = 7003; Version = "v4.0"; Mode = "Integrated"; Type = 3 }
    @{ Name = "EMR";            Port = 8005; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "EmrStatic";      Port = 8020; Version = "v4.0"; Mode = "Integrated"; Type = 2 }
    @{ Name = "Herp";           Port = 8007; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "his.baseapi";    Port = 8017; Version = "v4.0"; Mode = "Integrated"; Type = 3 }
    @{ Name = "MRMS";           Port = 8008; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "MRQC";           Port = 8009; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "OR";             Port = 8006; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "ORAPI";          Port = 7006; Version = "v4.0"; Mode = "Integrated"; Type = 3 }
    @{ Name = "PDS";            Port = 8004; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "PDSAPI";         Port = 7004; Version = "v4.0"; Mode = "Integrated"; Type = 3 }
    @{ Name = "Report";         Port = 8000; Version = "v4.0"; Mode = "Classic";    Type = 1 }
    @{ Name = "Sett";           Port = 8002; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
    @{ Name = "SettAPI";        Port = 7002; Version = "v4.0"; Mode = "Integrated"; Type = 3 }
    @{ Name = "Static";         Port = 8014; Version = "v4.0"; Mode = "Integrated"; Type = 2 }
    @{ Name = "Union";          Port = 8018; Version = "v4.0"; Mode = "Integrated"; Type = 1 }
)

# 2. 创建应用池和站点的函数
function Create-Site {
    param (
        [string]$Name,
        [string]$Path,
        [int]$Port,
        [string]$Version,
        [string]$Mode,
        [int]$Type
    )

    $appPoolPath = "IIS:\AppPools\$Name"

    # 创建或复用应用池
    if (-not (Test-Path $appPoolPath)) {
        New-WebAppPool -Name $Name
        Write-Host "创建应用池: $Name"
    } else {
        Write-Host "应用池 $Name 已存在"
    }

    # 应用池属性配置
    Set-ItemProperty $appPoolPath -Name managedRuntimeVersion -Value $Version
    Set-ItemProperty $appPoolPath -Name managedPipelineMode -Value $Mode
    Set-ItemProperty $appPoolPath -Name startMode -Value "AlwaysRunning"
    Set-ItemProperty $appPoolPath -Name "recycling.periodicRestart.schedule" -Value @("00:05", "12:05")
    Set-ItemProperty $appPoolPath -Name failure.rapidFailProtection -Value $false

    Start-WebAppPool -Name $Name

    # 删除已有站点（若存在）
    if (Get-Website -Name $Name -ErrorAction SilentlyContinue) {
        Stop-WebSite -Name $Name -ErrorAction SilentlyContinue
        Remove-WebSite -Name $Name -ErrorAction SilentlyContinue
        Write-Host "旧站点 $Name 已删除"
    }

    # 创建新站点
    New-Website -Name $Name -PhysicalPath $Path -ApplicationPool $Name -Port $Port -IPAddress "*"
    
    # 启用站点预加载
    Set-ItemProperty "IIS:\Sites\$Name" -Name applicationDefaults.preloadEnabled -Value $true

    Start-WebSite -Name $Name

    Write-Host "✅ [$Name] 应用池和站点创建完成。"

   # 延迟后进行站点唤醒（仅针对网页站点）
    Start-Sleep -Seconds 5

    if ($Type -eq 1) {
        try {
            $url = "http://localhost:$Port/"
            Invoke-WebRequest -Uri $url -UseBasicParsing | Out-Null
            Write-Host "🔄 [$Name] 网页服务预热请求已发送到 $url"
        } catch {
            Write-Warning "⚠️ [$Name] 预热失败：$($_.Exception.Message)"
        }
    } else {
        Write-Host "⏭ [$Name] 非网页服务（Type=$Type），已跳过预热"
    }
}

# 3. 遍历目录并匹配配置
Get-ChildItem -Path $directoryPath -Directory | ForEach-Object {
    $folderName = $_.Name
    $sitePath = Join-Path $absolutePath $folderName

    # 匹配配置
    $config = $configs | Where-Object { $_.Name -eq $folderName }

    if ($null -eq $config) {
        Write-Warning "⚠️ 未找到 [$folderName] 的配置，跳过"
        return
    }

    Create-Site -Name $config.Name -Path $sitePath -Port $config.Port -Version $config.Version -Mode $config.Mode -Type $config.Type
} 